import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/match/list/match_page_logic.dart';
import 'package:ball_lion/module/match/list/zq_match_filter_logic.dart';
import 'package:ball_lion/module/match/list/zq_view_logic.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/constant.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/loading/loading_match_list.dart';
import 'package:ball_lion/module/match/match_filter_float_widget.dart';
import 'package:ball_lion/module/match/soccer/zq_list_hot_cell.dart';
import 'package:ball_lion/module/match/soccer/zq_match_cell.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';
import 'package:ball_lion/widgets/header_delegate.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'package:ball_lion/utils/sp_utils.dart';
import 'package:ball_lion/widgets/ads_label.dart';

class ZqHotView extends StatefulWidget {
  const ZqHotView({super.key, required this.hotId, required this.title});

  final String title;
  final String hotId;

  @override
  State<ZqHotView> createState() => _ZqHotViewState();
}

class _ZqHotViewState extends State<ZqHotView> {
  late final ZqViewLogic logic;

  @override
  void initState() {
    logic = Get.put(
        ZqViewLogic(ZqMatchType.hot, Constant.matchFilterTagHot,
            hotId: widget.hotId, hotLabel: widget.title),
        tag: "${Constant.matchFilterTagHot}${widget.title}",
        permanent: true);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ZqViewLogic>(
      tag: "${Constant.matchFilterTagHot}${widget.title}",
      builder: (_) {
        return VisibilityDetector(
          key: Key(widget.hotId),
          onVisibilityChanged: (VisibilityInfo info) {
            if (info.visibleBounds.isEmpty) setState(() {});
          },
          child: Container(
            color: Colours.greyF2F3F7,
            child: Stack(
              alignment: Alignment.center,
              fit: StackFit.expand,
              children: [
                EasyRefresh.builder(
                  controller: logic.refreshC,
                  header:
                      Styles.refreshHeader(position: IndicatorPosition.locator),
                  onRefresh: () async {
                    Utils.onEvent('bspd_sx');
                    await logic.getMatch();
                  },
                  childBuilder: (BuildContext context, ScrollPhysics physics) =>
                      CustomScrollView(
                    physics: physics,
                    slivers: [
                      const HeaderLocator.sliver(),
                      if (SpUtils.matchZqAds?[0].hasValue == false ||
                          DateTime.now()
                                  .difference(
                                      DateTime.parse(SpUtils.matchZqAds![0]))
                                  .inHours >
                              24)
                        SliverToBoxAdapter(
                          child: AdsLabel(
                            adsTag: "match_zq_${widget.title}",
                            close: 0,
                            tapType: 'match_advertising',
                            tagParam: widget.title,
                            margin: const EdgeInsets.fromLTRB(8, 8, 8, 0),
                            onClose: () => setState(() {}),
                          ),
                        ),
                      LoadingWidget<int>(
                        data: logic.groupMatchList?.length ?? 0,
                        isLoading: logic.firstLoad,
                        loading: const LoadingMatchList.sliver(sportsId: 1),
                        noData: NoDataWidget.sliver(
                          tip: logic.quickFilter == QuickFilter.yiji
                              ? '暂无一级比赛'
                              : (logic.quickFilter == QuickFilter.jingcai
                                  ? '暂无竞足比赛'
                                  : '暂无比赛'),
                        ),
                        child: () => const SliverToBoxAdapter(),
                      ),
                      if ((logic.groupMatchList?.length ?? 0) > 0)
                        ...List.generate(
                            logic.dayList?.length ?? 0,
                            (index) => SliverMainAxisGroup(slivers: [
                                  SliverPersistentHeader(
                                      pinned: true,
                                      delegate: HeaderDelegate(
                                          extent: 28,
                                          child: Styles.matchListGroupHeader(
                                              logic.dayList![index]))),
                                  SliverList.builder(
                                      itemCount:
                                          logic.groupMatchList?[index].length ??
                                              0,
                                      itemBuilder: (BuildContext context, int i) =>
                                          logic.groupMatchList![index][i].hot ==
                                                      true &&
                                                  logic
                                                          .groupMatchList![index]
                                                              [i]
                                                          .isClose !=
                                                      true
                                              ? ZqListHotCell(
                                                  logic.groupMatchList![index]
                                                      [i],
                                                  logic)
                                              : ZqMatchCell(
                                                  logic.groupMatchList![index][i],
                                                  logic))
                                ])),
                      SliverToBoxAdapter(
                        child: Container(height: 80),
                      )
                    ],
                  ),
                ),
                Positioned(
                    bottom: 40,
                    child: MatchFilterFloatWidget(
                      0,
                      logic.firstLoad,
                      logic.quickFilter,
                      onTap: (index) {
                        logic.onSelectMatchType(index);
                      },
                    )),
                Styles.matchListBottomBar(
                    logic.hideMatchNum != 0 && !logic.isBottomHide,
                    logic.hideMatchNum, onClose: () {
                  Utils.onEvent('bspd_sxfc_gb');
                  logic.onCloseBottomBar();
                }, onDefault: () {
                  Utils.onEvent('bspd_sxfc_hf');
                  logic.onSelectMatchType(QuickFilter.all);
                  // update();
                })
              ],
            ),
          ),
        );
      },
    );
  }

  Widget matchList() {
    return CustomScrollView(
      slivers: List.generate(
          logic.dayList?.length ?? 0,
          (index) => SliverMainAxisGroup(slivers: [
                SliverPersistentHeader(
                    pinned: true,
                    delegate: HeaderDelegate(
                        extent: 28,
                        child: Styles.matchListGroupHeader(
                            logic.dayList![index]))),
                SliverList(
                    delegate: SliverChildBuilderDelegate(
                        childCount: logic.groupMatchList?[index].length ?? 0,
                        (context, i) => ZqMatchCell(
                            logic.groupMatchList![index][i], logic)))
              ])),
    );
  }
}
